using Microsoft.Data.SqlClient;

namespace DatabaseManager.Models;

/// <summary>
/// 数据库账号实体
/// </summary>
public class DatabaseAccount
{
    /// <summary>
    /// 登录名
    /// </summary>
    public string LoginName { get; set; } = string.Empty;

    /// <summary>
    /// 账号类型 (SqlLogin/WindowsLogin)
    /// </summary>
    public string AccountType { get; set; } = "SqlLogin";

    /// <summary>
    /// 账号状态
    /// </summary>
    public string Status { get; set; } = string.Empty;

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreateDate { get; set; }

    /// <summary>
    /// 修改时间
    /// </summary>
    public DateTime? ModifyDate { get; set; }

    /// <summary>
    /// 默认数据库
    /// </summary>
    public string DefaultDatabase { get; set; } = "master";

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsDisabled { get; set; }
}

/// <summary>
/// 数据库信息实体
/// </summary>
public class DatabaseInfo
{
    /// <summary>
    /// 数据库名称
    /// </summary>
    public string DatabaseName { get; set; } = string.Empty;

    /// <summary>
    /// 数据库ID
    /// </summary>
    public int DatabaseId { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreateDate { get; set; }

    /// <summary>
    /// 所有者
    /// </summary>
    public string Owner { get; set; } = string.Empty;

    /// <summary>
    /// 状态
    /// </summary>
    public string State { get; set; } = string.Empty;

    /// <summary>
    /// 恢复模式
    /// </summary>
    public string RecoveryModel { get; set; } = string.Empty;

    /// <summary>
    /// 兼容级别
    /// </summary>
    public int CompatibilityLevel { get; set; }

    /// <summary>
    /// 排序规则
    /// </summary>
    public string Collation { get; set; } = string.Empty;

    /// <summary>
    /// 数据库大小(MB)
    /// </summary>
    public decimal SizeMB { get; set; }
}

/// <summary>
/// 数据库权限实体
/// </summary>
public class DatabasePermission
{
    /// <summary>
    /// 登录名/用户名
    /// </summary>
    public string PrincipalName { get; set; } = string.Empty;

    /// <summary>
    /// 数据库名称
    /// </summary>
    public string DatabaseName { get; set; } = string.Empty;

    /// <summary>
    /// 权限类型 (SELECT, INSERT, UPDATE, DELETE, EXECUTE, etc.)
    /// </summary>
    public string PermissionType { get; set; } = string.Empty;

    /// <summary>
    /// 权限状态 (GRANT, DENY, REVOKE)
    /// </summary>
    public string PermissionState { get; set; } = string.Empty;

    /// <summary>
    /// 对象类型 (DATABASE, TABLE, VIEW, PROCEDURE, etc.)
    /// </summary>
    public string ObjectType { get; set; } = string.Empty;

    /// <summary>
    /// 对象名称
    /// </summary>
    public string? ObjectName { get; set; }

    /// <summary>
    /// 架构名称
    /// </summary>
    public string? SchemaName { get; set; }
}

/// <summary>
/// 创建账号请求
/// </summary>
public class CreateAccountRequest
{
    /// <summary>
    /// 登录名
    /// </summary>
    public string LoginName { get; set; } = string.Empty;

    /// <summary>
    /// 密码
    /// </summary>
    public string Password { get; set; } = string.Empty;

    /// <summary>
    /// 默认数据库
    /// </summary>
    public string DefaultDatabase { get; set; } = "master";

    /// <summary>
    /// 账号备注
    /// </summary>
    public string? Description { get; set; }
}

/// <summary>
/// 修改密码请求
/// </summary>
public class ChangePasswordRequest
{
    /// <summary>
    /// 登录名
    /// </summary>
    public string LoginName { get; set; } = string.Empty;

    /// <summary>
    /// 新密码
    /// </summary>
    public string NewPassword { get; set; } = string.Empty;
}

/// <summary>
/// 创建数据库请求
/// </summary>
public class CreateDatabaseRequest
{
    /// <summary>
    /// 数据库名称
    /// </summary>
    public string DatabaseName { get; set; } = string.Empty;

    /// <summary>
    /// 排序规则
    /// </summary>
    public string Collation { get; set; } = "Chinese_PRC_CI_AS";

    /// <summary>
    /// 恢复模式 (SIMPLE, FULL, BULK_LOGGED)
    /// </summary>
    public string RecoveryModel { get; set; } = "SIMPLE";

    /// <summary>
    /// 所有者
    /// </summary>
    public string? Owner { get; set; }
}

/// <summary>
/// 授予权限请求
/// </summary>
public class GrantPermissionRequest
{
    /// <summary>
    /// 登录名
    /// </summary>
    public string LoginName { get; set; } = string.Empty;

    /// <summary>
    /// 数据库名称
    /// </summary>
    public string DatabaseName { get; set; } = string.Empty;

    /// <summary>
    /// 权限类型 (ReadOnly, ReadWrite, DDLAdmin, Owner)
    /// </summary>
    public string PermissionLevel { get; set; } = "ReadOnly";

    /// <summary>
    /// 自定义权限列表
    /// </summary>
    public List<string>? CustomPermissions { get; set; }
}

/// <summary>
/// 撤销权限请求
/// </summary>
public class RevokePermissionRequest
{
    /// <summary>
    /// 登录名
    /// </summary>
    public string LoginName { get; set; } = string.Empty;

    /// <summary>
    /// 数据库名称
    /// </summary>
    public string DatabaseName { get; set; } = string.Empty;
}

/// <summary>
/// API响应结果
/// </summary>
public class ApiResult<T>
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 数据
    /// </summary>
    public T? Data { get; set; }

    public static ApiResult<T> SuccessResult(T data, string message = "操作成功")
    {
        return new ApiResult<T>
        {
            Success = true,
            Message = message,
            Data = data
        };
    }

    public static ApiResult<T> FailResult(string message)
    {
        return new ApiResult<T>
        {
            Success = false,
            Message = message
        };
    }
}
